package io.gitee.thghh.chronicon.swing.view;

import com.formdev.flatlaf.extras.FlatSVGIcon;
import io.gitee.thghh.chronicon.swing.components.WPanel;
import io.gitee.thghh.chronicon.swing.components.WScrollPane;
import io.gitee.thghh.chronicon.swing.utils.IconLoader;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

/**
 * @author wenli
 * @date 2024/05/09
 */
public class SidePane extends WPanel {


    private JXTreeTable treeTable;

    public SidePane() {
        initComponents();
        setDrawBoder(false);
    }

    private void initComponents() {
        DefaultMutableTreeTableNode root = new DefaultMutableTreeTableNode("root");
        root.add(new DefaultMutableTreeTableNode("模板配置"));
        root.add(new DefaultMutableTreeTableNode("类型配置"));
        root.add(new DefaultMutableTreeTableNode("数据源配置"));
        root.add(new DefaultMutableTreeTableNode("代码生成"));
        DefaultTreeTableModel tableModel = new DefaultTreeTableModel(root);

        // JXTable
        treeTable = new JXTreeTable(tableModel);
        treeTable.setShowHorizontalLines(true);
        treeTable.setOpaque(false);
        treeTable.setEditable(false);
        treeTable.setIntercellSpacing(new Dimension(1, 1));
        // 设置表头高度
        JTableHeader header = new JTableHeader() {
            @Override
            public Dimension getMaximumSize() {
                return new Dimension(super.getMaximumSize().width, 30);
            }
        };
        treeTable.setTableHeader(header);
        treeTable.setRowHeight(45);
        treeTable.setHighlighters(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, UIManager.getColor("App.hoverBackground"), null));

        treeTable.setTreeCellRenderer(new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                Component component = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                if (component instanceof JLabel) {
                    String icon = "icons/item.svg";
                    FlatSVGIcon svgIcon = IconLoader.getSvgIcon(icon, 25, 25);


                    ((JLabel) component).setIcon(svgIcon);
                    ((JLabel) component).setIconTextGap(7);

                }
                return component;
            }
        });
        treeTable.getColumnExt(0).setCellRenderer(new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                if (component instanceof JLabel) {
                    ((JLabel) component).setIcon(new FlatSVGIcon("icons/item.svg", 25, 25));
                }
                return component;
            }
        });
        WScrollPane jScrollPane = new WScrollPane(treeTable);
        jScrollPane.setPreferredSize(new Dimension(260, 0));
        this.setOpaque(false);
        this.setLayout(new BorderLayout());
        this.add(jScrollPane);
    }

    @Override
    public void updateUI() {

        super.updateUI();

        if (treeTable != null) {

            treeTable.setHighlighters(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, UIManager.getColor("App.hoverBackground"), null));

        }
    }
}
